package com.maxafaa.gravitydemo;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainSurfaceView extends SurfaceView  implements SurfaceHolder.Callback, Runnable{

	private boolean m_is_running;
	private SurfaceHolder m_surfaceholder;
	private Paint m_paint;
	private Thread m_mainthread;
	private float m_pointer_press_x, m_pointer_press_y, m_pointer_move_x, m_pointer_move_y;
	private double m_shot_power;
	private final int M_MAX_POWER = 100;
	private double m_shot_angle;
	private ArrayList<PhysicObject> m_bird_list;
	
	
	public MainSurfaceView(Context context) {
		super(context);
		m_surfaceholder = getHolder();
		m_surfaceholder.addCallback(this);		
		setFocusable(true);	
	}

	private void Initialize(){
		
		m_pointer_press_x = -1;
		m_pointer_press_y = -1;
		m_pointer_move_x = -1;
		m_pointer_move_y = -1;
		m_bird_list = new ArrayList<PhysicObject>();
		m_paint = new Paint();
		m_mainthread = new Thread(this);
		m_mainthread.start();
		m_is_running = true;
		
		
	}
	
	private void GameUpdate(){
		for(int i = 0; i < m_bird_list.size(); i ++){
			m_bird_list.get(i).Update();
		}
	}
	
	public void GameDestroy(){
		RemoveAllBird();
		m_pointer_press_x = -1;
		m_pointer_press_y = -1;
		m_pointer_move_x = -1;
		m_pointer_move_y = -1;
		m_bird_list = null;
		m_paint = null;		
	}	
	
	public boolean RemoveAllBird(){		
		if(m_bird_list.size() > 0){
			for(int i = m_bird_list.size() - 1; i >= 0; i --){
				m_bird_list.get(i).Destroy();
				m_bird_list.remove(i);
			}
			return true;
		}else{
			return false;
		}
	}
	
	private void GameDraw(Canvas canvas){
		canvas.drawColor(Color.WHITE);
		m_paint.setColor(Color.BLACK);
		canvas.drawText("Object Count: " + m_bird_list.size(), 15, 20, m_paint);
		canvas.drawText("Last X: "+ (int)m_pointer_press_x, 15, 35, m_paint);
		canvas.drawText("Last Y: "+ (int)m_pointer_press_y, 15, 50, m_paint);		
		canvas.drawText("Last Power: " + m_shot_power, 15, 65, m_paint);
		canvas.drawText("Last Angle: " + Math.toDegrees(m_shot_angle), 15, 80, m_paint);
		m_paint.setColor(Color.RED);
		canvas.drawLine(m_pointer_press_x, m_pointer_press_y, m_pointer_move_x, m_pointer_move_y, m_paint);
		m_paint.setAntiAlias(true);
		m_paint.setColor(Color.BLUE);
		for(int i = 0; i < m_bird_list.size(); i ++){
			canvas.drawCircle(m_bird_list.get(i).getPositionX(), m_bird_list.get(i).getPositionY(), m_bird_list.get(i).getRadius(), m_paint);			
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e){
		super.onTouchEvent(e);		
		if(e.getActionMasked() == MotionEvent.ACTION_UP || e.getActionMasked() == MotionEvent.ACTION_POINTER_UP){			
			m_shot_angle = Math.atan2((double)(m_pointer_move_x - m_pointer_press_x), (double)(m_pointer_move_y - m_pointer_press_y));			
			m_shot_angle += Math.toRadians(90);
			m_shot_power = Math.sqrt((double)((m_pointer_move_x - m_pointer_press_x)*(m_pointer_move_x - m_pointer_press_x) + (m_pointer_move_y - m_pointer_press_y)*(m_pointer_move_y - m_pointer_press_y)));
			if(m_shot_power > M_MAX_POWER){
                m_shot_power = M_MAX_POWER;
                m_pointer_move_x = (int)(m_pointer_press_x - (m_shot_power * Math.cos(m_shot_angle)));
                m_pointer_move_y = (int)(m_pointer_press_y + (m_shot_power * Math.sin(m_shot_angle)));
            }
			m_bird_list.add(new PhysicObject((int)m_pointer_press_x, (int)m_pointer_press_y, m_shot_power, m_shot_angle, 0, 0, getWidth(), getHeight()));
			m_bird_list.get(m_bird_list.size() - 1).setRadius(25);
		}
		if(e.getActionMasked() == MotionEvent.ACTION_DOWN || e.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN){			
			m_pointer_press_x = e.getX();
			m_pointer_press_y = e.getY();
		}
		if(e.getActionMasked() == MotionEvent.ACTION_MOVE){
			m_pointer_move_x = e.getX();
			m_pointer_move_y = e.getY();
			m_shot_angle = Math.atan2((double)(m_pointer_move_x - m_pointer_press_x), (double)(m_pointer_move_y - m_pointer_press_y));			
			m_shot_angle += Math.toRadians(90);
			m_shot_power = Math.sqrt((double)((m_pointer_move_x - m_pointer_press_x)*(m_pointer_move_x - m_pointer_press_x) + (m_pointer_move_y - m_pointer_press_y)*(m_pointer_move_y - m_pointer_press_y)));
			if(m_shot_power > M_MAX_POWER){
                m_shot_power = M_MAX_POWER;
                m_pointer_move_x = (int)(m_pointer_press_x - (m_shot_power * Math.cos(m_shot_angle)));
                m_pointer_move_y = (int)(m_pointer_press_y + (m_shot_power * Math.sin(m_shot_angle)));
            }
		}
		return true;
	}

	@Override
	public void run() {
		while(m_is_running){
			try{
				// Thread.sleep(20);
			}catch(Exception e){}
			GameUpdate();
			Canvas m_lockcanvas = m_surfaceholder.lockCanvas(null);
			synchronized (m_surfaceholder) {
				if(m_lockcanvas != null){
					GameDraw(m_lockcanvas);
					m_surfaceholder.unlockCanvasAndPost(m_lockcanvas);
				}
			}
		}		
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		Initialize();
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		boolean retry = true;
		m_is_running = false;
        do {
            try {
                m_mainthread.join();
                retry = false;
            } catch (InterruptedException e) {
				e.printStackTrace();
                // we will try it again and again...
            }
        }while(retry);		
	}
}
